package sggJuc.future;

import java.util.concurrent.*;

/**
 * CompletableFuture之四大静态方法
 *
 * @author xizheng
 * @date 2023-08-08 09:57:37
**/
public class CompletableFutureBuildDemo {
    public static void main(String[] args) {
        //添加线程名

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //1、 runAsync 方法
/*        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },executorService);

        try {
            System.out.println(voidCompletableFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }*/


        //2、supplyAsync 方法
        CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "hello supplyAsync";
        },executorService);
        try {
            System.out.println(uCompletableFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        executorService.shutdown();
    }
}
